package com.tl.web.controller.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tl.app.domain.AppOrder;
import com.tl.app.domain.AppOrderPriceConfig;
import com.tl.app.service.IAppOrderPriceConfigService;
import com.tl.app.service.IAppOrderService;
import com.tl.common.annotation.Log;
import com.tl.common.core.controller.BaseController;
import com.tl.common.core.domain.AjaxResult;
import com.tl.common.core.page.TableDataInfo;
import com.tl.common.enums.BusinessType;
import com.tl.common.utils.IPUtil;


/**
 * 
 * @author 15110
 *
 */
@RestController
@RequestMapping("/api/order")
public class ApiOrderController extends BaseController{
	
	
	 @Autowired
	 private IAppOrderPriceConfigService appOrderPriceConfigService;
	 
	 @Autowired
	 private IAppOrderService appOrderService;
	 
	 
	/**
     * 查询订单价格配置列表
     */
    @GetMapping("/getOrderPriceConfigList")
    public AjaxResult getOrderPriceConfigList(AppOrderPriceConfig appOrderPriceConfig)
    {
        List<AppOrderPriceConfig> list = appOrderPriceConfigService.selectAppOrderPriceConfigList(appOrderPriceConfig);
        return AjaxResult.success(list);
    }
	
    /**
     * 新增订单并发起支付
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/payOrder")
    public AjaxResult payOrder(HttpServletRequest req,@RequestBody AppOrder appOrder){
    	appOrder.setCreateBy(getUsername());
    	appOrder.setXdUserId(getUserId());
    	Map<String, Object> ao=appOrderService.payOrder(req,appOrder);
        return AjaxResult.success(ao);
    }
    
    
    /**
     * 查询随机订单列表
     */
    @GetMapping("/getRandomOrderList")
    public AjaxResult getRandomOrderList(AppOrder appOrder){
        appOrder.setFwUserId(getUserId());
        List<Map<String,Object>> list = appOrderService.selecRandomOrderList(appOrder);
        List<Map<String,Object>> resultList =new ArrayList<Map<String,Object>>(); 
        for(Map<String,Object> map:list) {
        	if(map.get("isPC").equals("1")&&!map.get("isJg").equals("0")) {
        		continue;
        	}else {
        		resultList.add(map);
        	}
        }
        return AjaxResult.success(resultList);
    }
    
    /**
     * 查询订单列表
     */
    @GetMapping("/getUserOrderList")
    public TableDataInfo getUserOrderList(AppOrder appOrder)
    {
        startPage();
        appOrder.setXdUserId(getUserId());
        List<AppOrder> list = appOrderService.selectAppOrderList(appOrder);
        return getDataTable(list);
    }
    
    
    /**
     * 查询打赏订单列表
     */
    @GetMapping("/getRewardOrderList")
    public TableDataInfo getRewardOrderList(AppOrder appOrder){
        startPage();
        List<AppOrder> list = appOrderService.selectAppOrderList(appOrder);
        return getDataTable(list);
    }
    
    
    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/getInfo/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(appOrderService.selectAppOrderByOrderId(orderId));
    }
    
    
    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(appOrderService.deleteAppOrderByOrderIds(orderIds));
    }
    
    /**
     * 查询订单排名
     */
    @GetMapping("/getAppOrderRnak")
    public TableDataInfo getAppOrderRnak(String type){
        startPage();
        List<Map<String, Object>> list = appOrderService.selectAppOrderRnak(type);
        return getDataTable(list);
    }

}
